package com.example.fw;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.openqa.selenium.By;

public class AccountHelper extends HelperWithWebDriverBase {

	public AccountHelper(ApplicationManager applicationManager) {
		super(applicationManager);
	}

	public void signup(User user) {
		openUrl("");
		//click(By.cssSelector("span.bracket-link"));
		findElement(By.xpath("(//span/a[@href='signup_page.php'])")).click();	//<input type="submit" value="Delete" name="update">
		type(By.name("username"), user.login);
		type(By.name("email"), user.email);
		click(By.cssSelector("input.button"));
		
		pause(3000);
		String msg = manager.getMailHelper().getNewMail(user.login, user.password);
		String confirmationLink = getConfirmationLink(msg);
		openAbsoluteUrl(confirmationLink);
		
		type(By.name("password"), user.password);
		type(By.name("password_confirm"), user.password);
		click(By.cssSelector("input.button"));
	}

	// lesson 9 - 52:50
	public String getConfirmationLink(String text) {
		Pattern regex = Pattern.compile("http\\S*");	//  \\S - is not space 
		Matcher matcher = regex.matcher(text);
		if (matcher.find()) {
			return matcher.group();
		} else {
			return "";
		}
	}

	public boolean isLogged(User user) {
		// TODO Auto-generated method stub
		return false;
	}
}